# Contributor: The one with the braid <info@braid.business>
# Maintainer: The one with the braid <info@braid.business>
pkgname=polycule
pkgver=0.0.3
pkgrel=1
pkgdesc="A geeky and efficient [matrix] client for power users."
url="https://gitlab.com/polycule_client/polycule"
arch="aarch64 x86_64"	# flutter
license="EUPL-1.2"
# libsecret: pub/flutter_secure_storage
# xdg-user-dirs: pub/path_provider
# olm: pub/matrix
# mimalloc2: pub/media_kit
# libnotify: pub/flutter_local_notifications
# dbus: pub/flutter_local_notifications
# openssl-dev: pub/sqlcipher_flutter_libs
# mpv-dev: pub/media_kit
depends="
	jsoncpp
	libsecret
	xdg-user-dirs
	olm
	mimalloc2
	libnotify
	dbus
	"
makedepends="
	ca-certificates
	flutter-desktop
	libsecret-dev
	patchelf
	openssl-dev
	mpv-dev
	"
source="
	https://gitlab.com/polycule_client/polycule/-/archive/${_rev:-"v$pkgver"}/polycule-${_rev:-v"$pkgver"}.tar.gz
	system-flutter.patch
	no-werror.patch
	media-kit-broken-by-design.patch.pubdev
	"
builddir="$srcdir/polycule-${_rev:-"v$pkgver"}"
# net: pub dependencies
# !check: no tests
options="net !check"

export _pkgorg="business.braid.polycule"
export _pkgexec="polycule"

case "$CARCH" in
	aarch64) _flutter_arch="arm64" ;;
	x86_64) _flutter_arch="x64" ;;
esac

export PUB_CACHE="$srcdir/pub_cache"

export CFLAGS="$CFLAGS -O2 -Wno-error -Wno-unknown-warning-option -Wno-unused-command-line-argument"
export CXXFLAGS="$CXXFLAGS -O2 -Wno-error -Wno-unknown-warning-option -Wno-unused-command-line-argument"

export CC=clang
export CXX=clang++
export AR=llvm-ar
export NM=llvm-nm
export LD=clang++

prepare() {
	default_prepare

	flutter pub get --enforce-lockfile
	flutter gen-l10n

	# if there is a binary in the pub_cache, we sure did not build it.
	for elf in $(scanelf -RA -F "%F" "$PUB_CACHE"); do
		rm -f "$elf"
	done

	# credits to jane400 <alpine@j4ne.de>
	cd "$srcdir"
	patch -p1 -i "$srcdir/media-kit-broken-by-design.patch.pubdev"
}

build() {
	flutter build linux -v --release \
		--dart-define=POLYCULE_IS_STABLE=true \
		--dart-define=POLYCULE_VERSION=v$pkgver

	local rpath="/usr/lib/$pkgname"

	patchelf --set-rpath "$rpath/lib" build/linux/"$_flutter_arch"/release/bundle/$_pkgexec
	for _elf in build/linux/"$_flutter_arch"/release/bundle/lib/lib*.so; do
		# running patchelf on libapp (the AOT snapshot of the whole Dart codebase)
		# breaks it with "[FATAL:flutter/runtime/dart_vm_initializer.cc(89)]
		# Error while initializing the Dart VM: Invalid vm isolate snapshot seen".
		#
		# it has no rpath and only links to libc soname anyway. 🤷‍♀️
		[ "$(basename "$_elf")" != "libapp.so" ] || continue

		patchelf --set-rpath "$rpath" "$_elf"
	done
}

package() {
	local bundle="$builddir"/build/linux/$_flutter_arch/release/bundle

	install -Dm755 "$bundle"/$_pkgexec "$pkgdir"/usr/lib/$pkgname/$_pkgexec
	cp -rv "$bundle"/lib "$pkgdir"/usr/lib/$pkgname/lib
	cp -rv "$bundle"/data "$pkgdir"/usr/lib/$pkgname/data

	mkdir -p "$pkgdir"/usr/bin
	ln -sv /usr/lib/$pkgname/$_pkgexec "$pkgdir"/usr/bin/$_pkgexec

	install -Dm644 "$bundle"/data/flutter_assets/NOTICES.Z "$pkgdir"/usr/share/licenses/$pkgname/NOTICES.Z
	install -Dm644 "$builddir"/LICENSE "$pkgdir"/usr/share/licenses/$pkgname/COPYING
	for font in "$builddir"/assets/fonts/* ; do
		install -Dm644 "$font"/LICENSE.txt "$pkgdir"/usr/share/licenses/$pkgname/"$(basename "$font")".txt
	done
	install -Dm644 linux/$_pkgorg.desktop "$pkgdir"/usr/share/applications/$_pkgorg.desktop
	install -Dm644 linux/$_pkgorg-daemon.desktop "$pkgdir"/etc/xdg/autostart/$_pkgorg-daemon.desktop
	install -Dm644 linux/$_pkgorg.service "$pkgdir"/usr/share/dbus-1/services/$_pkgorg.service
	install -Dm644 linux/$_pkgorg.metainfo.xml "$pkgdir"/usr/share/metainfo/$_pkgorg.metainfo.xml
	install -Dm644 assets/logo/logo-circle.svg "$pkgdir"/usr/share/icons/hicolor/scalable/apps/$_pkgorg.svg
}

sha512sums="
960d81a34393621f6dc41c7ae1157655f806027f4a7e8ee5fed36572c60971fdee0a83658ca1732fde92226a3475cca346d4b5b3015380eed3661f696712779a  polycule-v0.0.3.tar.gz
00108c868dbb776ca4ce41861b31dd3e833b691d26f25a23c9b7a619be262f433ec655158c3e2e8dcb7c16ff900baf9e6eb51f7edadb945821df841e5e6b8bc2  system-flutter.patch
f1f9ed7c023024b11ea7ed4bfb02c8333e41c010b12c3ded816aae9ae947b8ccd0c9d2ca1e870fa92291553e75c7ba0b24c7be6e2d340c9d8de6a95f0d3410c8  no-werror.patch
159e717794811368df94401b0b18af01c835ac0da6b772026e95090699338cf10d05d494151f8c51409cf8440e32eeaba2d28f632ac3c288d5a7da4fa91f0b71  media-kit-broken-by-design.patch.pubdev
"
